#ifndef _THTEAD_H_
#define _THTEAD_H_
namespace share
{

class Thread
{
private:
	class ThreadFunctorBase
	{
	public:
		virtual void invoke( ) = 0;
	};

	template <class TFUNC>
	struct invoker
	{
		void operator( )( TFUNC &func )
		{
			func( );
		}
	};

	template <class TOBJPTR, class TOBJ_FUNC_PTR>
	struct invoker< std::pair< TOBJPTR, TOBJ_FUNC_PTR > >
	{
		void operator( )( std::pair<TOBJPTR, TOBJ_FUNC_PTR> &func )
		{
			( func.first->*func.second )( );
		}
	};
	template <class TFUNC> class ThreadFunctor: public ThreadFunctorBase
	{
	public:
		ThreadFunctor( const TFUNC &func )
			: func_( func )
		{

		}
		virtual void invoke( )
		{
			invoker < TFUNC > ( )( func_ );
		}
	private:
		TFUNC func_;
	};

public:

	template <class TFUNC>
	Thread( TFUNC func )
	{
		thread_func( func );
		start( );
	}

	template <class TOBJPTR, class TOBJ_FUNC_PTR>
	Thread( TOBJPTR objptr, TOBJ_FUNC_PTR func )
	{
		thread_func( objptr, func );
		start( );
	}

	Thread( void );

	~Thread( void );

	void start( );

	void join( );

	template <class TFUNC>
	void thread_func( TFUNC func )
	{
		thread_func_ = new ThreadFunctor<TFUNC> ( func );
	}

	template <class TOBJPTR, class TOBJ_FUNC_PTR>
	void thread_func( TOBJPTR objptr, TOBJ_FUNC_PTR func )
	{
		thread_func( std::pair<TOBJPTR, TOBJ_FUNC_PTR> ( objptr, func ) );
	}

	ThreadHandle thread_id( ) const
	{
		return this->thread_id_;
	}

private:

	ThreadFunctorBase *thread_func_;

	ThreadHandle thread_id_;

private:
	static THRFUNC_RETURN __STDCALL warpped_fun( void *param );
};

}
#endif
